package com.home766.controller.admin;

import com.home766.dto.DictionaryDTO;
import com.home766.entity.Dictionary;
import com.home766.entity.OrderReward;
import com.home766.service.IDictionaryService;
import com.home766.service.OrderRewardService;
import net.biz.Message;
import net.biz.Pageable;
import net.biz.controller.admin.BaseController;
import net.biz.entity.Brand;
import net.biz.entity.ProductCategory;
import net.biz.service.AdminService;
import net.biz.service.BrandService;
import net.biz.service.ProductCategoryService;
import net.biz.util.JsonUtils;
import net.biz.util.ParamsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * 门店下单奖励计算提成点维护
 */

@Controller
@RequestMapping("/admin/orderReward")
public class OrderRewardController extends BaseController {
	
	@Resource(name="orderRewardServiceImpl")
	private OrderRewardService orderRewardService;
	@Autowired
	private IDictionaryService iDictionaryService;
	@Resource(name="brandServiceImpl")
	private BrandService brandService;
	@Resource(name="productCategoryServiceImpl")
	private ProductCategoryService productCategoryService;
	@Resource(name="adminServiceImpl")
	private AdminService adminService;
	
	/**
	 * 列表查询
	 * @param model
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(ModelMap model, Pageable pageable){
		if(pageable.getOrderProperty()==null){
			pageable.setOrderProperty("productCategory");
		}
		model.addAttribute("page", orderRewardService.findPage(pageable));
		if(pageable.getOrderProperty().equals("schemeCategory")){
			pageable.setOrderProperty("productCategory");
		}else{
			pageable.setOrderProperty("schemeCategory");
		}
		model.addAttribute("category",pageable.getOrderProperty());
		return "/admin/order_reward/list";
	}
	
	/**
	 * 新增
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.GET)
	public String add(ModelMap model){
		model.addAttribute("brands", brandService.findAll());
		List<ProductCategory> productCategorys = productCategoryService.findRoots();
		List<DictionaryDTO> schemeCategorys = iDictionaryService.searchChildren(ParamsUtil.SCHEME_TYPE);
		model.addAttribute("schemeCategorys", schemeCategorys);
		model.addAttribute("productCategorys", productCategorys);
		model.addAttribute("schemeCategorysData", JsonUtils.toJson(schemeCategorys));
		model.addAttribute("productCategorysData", JsonUtils.toJson(productCategorys));
		return "/admin/order_reward/add";
	}
	
	/**
	 * 编辑
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/edit",method = RequestMethod.GET)
	public String edit(ModelMap model, Long id){
		if(id == null){
			return ERROR_VIEW;
		}
		OrderReward reward = orderRewardService.find(id);
		if(reward == null){
			return ERROR_VIEW;
		}
		if(reward.getType() == null){
			return ERROR_VIEW;
		}
		model.addAttribute("reward", reward);
		model.addAttribute("brands", brandService.findAll());
		List<ProductCategory> productCategorys = productCategoryService.findRoots();
		List<DictionaryDTO> schemeCategorys = iDictionaryService.searchChildren(ParamsUtil.SCHEME_TYPE);
		model.addAttribute("productCategorys", productCategorys);
		model.addAttribute("productCategorysData", JsonUtils.toJson(productCategorys));
		model.addAttribute("schemeCategorys", schemeCategorys);
		model.addAttribute("schemeCategorysData", JsonUtils.toJson(schemeCategorys));
		return "/admin/order_reward/edit";
	}
	
	/**
	 * 查看
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/view",method = RequestMethod.GET)
	public String view(ModelMap model, Long id){
		if(id == null){
			return ERROR_VIEW;
		}
		OrderReward reward = orderRewardService.find(id);
		if(reward == null){
			return ERROR_VIEW;
		}
		model.addAttribute("reward", reward);
		return "/admin/order_reward/detail";
	}
	
	/**
	 * 保存
	 * @param type
	 * @param productCategory
	 * @param schemeCategory
	 * @param brand
	 * @param point
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/save",method = RequestMethod.POST)
	public String save(Integer type, Long category, Long brand, Double point, RedirectAttributes redirectAttributes){
		if(type == null || brand == null || point == null || category == null){
			return ERROR_VIEW;
		}
		Brand bra = brandService.find(brand);
		if(bra == null){
			return ERROR_VIEW;
		}
		OrderReward reward = new OrderReward();
		reward.setType(type);
		reward.setBrand(bra);
		reward.setPoint(point);
		reward.setCreatePerson(adminService.getCurrentUsername());
		if(type == ParamsUtil.CART_TYPE_NORMAL){
			ProductCategory pc = productCategoryService.find(category);
			if(pc == null){
				return ERROR_VIEW;
			}
			reward.setProductCategory(pc);
		}else {
			Dictionary sc = iDictionaryService.find(category);
			if(sc == null){
				return ERROR_VIEW;
			}
			reward.setSchemeCategory(sc);			
		}
		try {
			orderRewardService.save(reward);
			addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		} catch (Exception e) {
			e.printStackTrace();
			addFlashMessage(redirectAttributes, ERROR_MESSAGE);
			return "redirect:add.jhtml";
		}
		return "redirect:list.jhtml";
	}
	
	/**
	 * 更新
	 * @param id
	 * @param type
	 * @param productCategory
	 * @param schemeCategory
	 * @param brand
	 * @param point
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public String update(Long id, Integer type, Long category, Long brand, Double point, RedirectAttributes redirectAttributes){
		if(id == null || type == null || brand == null || point == null || category == null){
			return ERROR_VIEW;
		}
		OrderReward or = orderRewardService.find(id);
		if(or == null){
			return ERROR_VIEW;
		}
		Brand bra = brandService.find(brand);
		if(bra == null){
			return ERROR_VIEW;
		}
		or.setType(type);
		or.setBrand(bra);
		or.setPoint(point);
		if(type == ParamsUtil.CART_TYPE_NORMAL){
			ProductCategory pc = productCategoryService.find(category);
			if(pc == null){
				return ERROR_VIEW;
			}
			or.setProductCategory(pc);
			or.setSchemeCategory(null);			
		}else {
			Dictionary sc = iDictionaryService.find(category);
			if(sc == null){
				return ERROR_VIEW;
			}
			or.setSchemeCategory(sc);			
			or.setProductCategory(null);
		}
		try {
			orderRewardService.update(or);
			addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		} catch (Exception e) {
			e.printStackTrace();
			addFlashMessage(redirectAttributes, ERROR_MESSAGE);
			return "redirect:edit.jhtml";
		}
		return "redirect:list.jhtml";
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="/delete",method = RequestMethod.POST)
	@ResponseBody
	public Message delete(Long[] ids){
		if(ids == null || ids.length == 0){
			return ERROR_MESSAGE;
		}
		try {
			orderRewardService.delete(ids);
			return SUCCESS_MESSAGE;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR_MESSAGE;
		}
	}
	
}
